/**
 * @name:music Player
 * @user:icodess
 * @url:http://www.3lian.com/edu/2015/04-25/207655.html
 */
var Player = {
	boxEle:"Player",//主盒子id参数
	audioEle:"audio",//播放器domid
	sourceEle:"source",//资源id
	musicBox:null,//播放器dom
	audio:null,//播放器dom
	source:null,//资源
	playBtnEle:"m-play",//播放按钮
	prevBtnEle:"m-prev",//上一首按钮
	nextBtnEle:"m-next",//下一首按钮
	musicNameEle:'m-title',//音乐标题
	nusicCoverEle:'m-cover',//音乐封面
	musicSingerEle:'m-singer',//音乐歌手
	musicSinger:null,
	playBtn:null,
	prevBtn:null,
	nextBtn:null,
	musicName:null,
	musicCover:null,

	currentTimeEle:'current-time',//当前时间
	totalTimeEle:'total-time',//总时间
	progressBarEle:'bar-limit',//进度条
	bufferBarEle:'buffer-limit',//缓冲进度
	currentTime:null,
	totalTime:null,
	progressBar:null,
	bufferBar:null,

	interval:null,//进度条定时器

	musicList:[],//歌曲播放列表
	currentMusic:{},//当前播放歌曲对象
	currentMusicIndex:null,//当前音乐索引
	playModeList:[
		'<i class="glyphicon glyphicon-arrow-right"></i>',
		'<i class="glyphicon glyphicon-retweet"></i>',
		'<i class="glyphicon glyphicon-random"></i>'
	],//播放模式 1 顺序循环 2 单曲循环 3随机
	playModeIndex:0,//当前播放模式
	playTypeEle:"play-type",
	playType:null,//播放模式切换dom

	addVolEle:"add-vol",
	reduceVolEle:"reduce-vol",
	addVol:null,
	reduceVol:null,
}

//初始化
Player.init = function(options,mList=[]){
	this.musicBox = document.getElementById(this.boxEle);
	this.progressBar = document.getElementById(this.progressBarEle);
	this.bufferBar = document.getElementById(this.bufferBarEle);
	this.currentTime = document.getElementById(this.currentTimeEle);
	this.totalTime = document.getElementById(this.totalTimeEle);
	this.musicName = document.getElementById(this.musicNameEle);
	this.musicCover = document.getElementById(this.nusicCoverEle);
	this.musicSinger = document.getElementById(this.musicSingerEle);
	this.playBtn = document.getElementById(this.playBtnEle);
	this.prevBtn = document.getElementById(this.prevBtnEle);
	this.nextBtn = document.getElementById(this.nextBtnEle);

	this.playType = document.getElementById(this.playTypeEle);
	this.addVol = document.getElementById(this.addVolEle);
	this.reduceVol = document.getElementById(this.reduceVolEle);

	
	this.createAudio();//创建
	this.bind();//绑定事件
	this.addMusicList(mList);//绑定音乐列表
	return Player;
}
//绑定事件
Player.bind = function(){
	this.playBtn = document.getElementById(this.playBtnEle);
	var playBtn = this.playBtn;
	var _that = this;
	this.playBtn.addEventListener('click',function(){
		_that.playOrPaused(playBtn);
	});//绑定播放事件
	this.prevBtn.addEventListener('click',function(){
		_that.prevOrNext(-1);
	});//绑定上一首事件
	this.nextBtn.addEventListener('click',function(){
		_that.prevOrNext(1);
	});//绑定下一首事件
	this.playType.addEventListener('click',function(){
		_that.playModeCheck();
	});//绑定播放模式事件
	this.addVol.addEventListener('click',function(){
		_that.setVol(1);
	});//绑定加音量事件
	this.reduceVol.addEventListener('click',function(){
		_that.setVol(-1);
	});//绑定减音量事件
}
//添加音乐列表
Player.addMusicList = function(musicArr=[]){
	this.musicList = musicArr;
	if(musicArr.length>=1){
		this.currentMusic = this.musicList[0];
		this.changeSource(this.musicList[0]);
		this.currentMusicIndex = 0;
	}
}
//播放模式改变
Player.playModeCheck = function(){
	var modeNum = this.playModeList.length;
	var index = this.playModeIndex;
	if(index>=modeNum-1){
		index = 0;
	}else{
		index += 1;
	}
	this.playModeIndex = index;
	this.playType.innerHTML = this.playModeList[index];
}
//上一首或者下一首
Player.prevOrNext = function(type){
	//音乐数量
	var total = this.musicList.length;
	//type == 1 下一首  type==-1 上一首
	var index = this.currentMusicIndex;
	//循环的情况下
	var newIndex = 0;
	if((index>0) && (index < total-1)){
		newIndex = index+type;
	}else{
		if((index+type) < 0){
			newIndex = total-1;
		}else if((index+type)==total){
			newIndex = 0;
		}else{
			newIndex = index+type;
		}
	}
	this.currentMusic = this.musicList[newIndex];
	this.changeSource(this.musicList[newIndex]);
	this.currentMusicIndex = newIndex;
	var boolPaused = this.audio.paused;
	this.initProgress()//初始化进度条
	this.audio.load();//重新加载
	if(boolPaused){
		this.audio.pause();
	}else{
		this.audio.play();
	}
}
//进度条初始化
Player.initProgress = function(){
	var tTime = this.getTotalTime();
	this.currentTime.innerHTML = '0:0:0';
	this.totalTime.innerHTML = this.formetTime(tTime);;
	this.progressBar.style.width = '0%';
	this.bufferBar.style.width = '0%';
}
//开始显示进度
Player.startProgress = function(){
	var _that = this;
	this.interval = setInterval(function(){
		var cTime = _that.getCurrentTime();
		var tTime = _that.getTotalTime();
		_that.currentTime.innerHTML = _that.formetTime(cTime);
		_that.totalTime.innerHTML = _that.formetTime(tTime);
		var progressLenght = (cTime/tTime)*100;
		progressLenght = progressLenght.toFixed(2);
		_that.bufferProgress();//缓冲进度
		_that.progressBar.style.width = progressLenght+'%';

		//判断是否结束
		if(_that.audio.ended){
			//切换下一首
			_that.prevOrNext(1);
			this.audio.play();
		}
	},100);
}
//停止进度
Player.stopProgress = function(){
	clearInterval(this.interval);
}
//缓冲进度
Player.bufferProgress = function(){
	var timeRanges = this.audio.buffered;
	 //获取已经缓存的时间
	var timeBuffered = timeRanges.end(timeRanges.length - 1);
	 //获取缓存进度，值为0到1
	var bufferPercent = timeBuffered/this.audio.duration; 
	//设置缓冲进度
	this.bufferBar.style.width = bufferPercent+'%';
}
//播放或者暂停
Player.playOrPaused = function(obj){
	if(this.audio.paused){
		this.audio.play();
		this.startProgress();
		obj.innerHTML='<i class="glyphicon glyphicon-pause"></i>';
		return;
	}
	this.stopProgress();
	this.audio.pause();
	obj.innerHTML='<i class="glyphicon glyphicon-play"></i>';
}
//获取当前播放时间
Player.getCurrentTime = function(){
	return parseInt(this.audio.currentTime);
}
//获取总时间
Player.getTotalTime = function(){
	return parseInt(this.audio.duration);
}
//音量控制
Player.setVol = function(type,obj){
	if(type == 1){
		var volume = this.audio.volume + 0.1;
		if(volume >=1 ){
			volume = 1 ;
		}
		this.audio.volume = volume;
	}else if(type == -1){
		var volume = this.audio.volume - 0.1;
		if(volume <=0 ){
			volume = 0 ;
		}
		this.audio.volume = volume;
	}else{
		this.muted();
	}
	//document.getElementById('nowVol').innerHTML = returnFloat(this.audio.volume);
}
//静音
Player.muted = function(obj){
	if(this.audio.muted){
		this.audio.muted = false;
		obj.innerHTML = '开启静音';
	}else{
		this.audio.muted = true; 
		obj.innerHTML = '关闭静音';
	}
}


//创建audio Dom节点
Player.createAudio = function(){
	this.audio = document.createElement("audio");
	//this.audio.setAttribute('controls',true);
	this.audio.setAttribute('id',this.audioEle);
	this.musicBox.appendChild(this.audio);
	//创建资源
	this.createSource();
}
//创建资源
Player.createSource = function(){
	//src="music.mp3" type="audio/mpeg" codecs="mp3"
	this.source = document.createElement("source");
	this.source.setAttribute('id',this.sourceEle);
	this.source.setAttribute('type','audio/mpeg');
	this.source.setAttribute('codecs','mp3');
	this.source.setAttribute('src','');
	this.audio.appendChild(this.source);
}
//修改播放文件
Player.changeSource = function(source){
	this.source.setAttribute('type',source.type);
	this.source.setAttribute('codecs',source.codecs);
	this.source.setAttribute('src',source.src);
	this.musicName.innerHTML = source.title;//标题
	this.musicSinger.innerHTML = source.singer;//歌手
	this.musicCover.setAttribute('src',source.cover);//封面
	return this.source;
}
//保留一位小数
Player.returnFloat = function(value) { 
	value = Math.round(parseFloat(value) * 10) / 10;
	if (value.toString().indexOf(".") < 0){
		value = value.toString() + ".0";
	}
	return value;
}
//时间转换[秒 转为 时分秒]
Player.formetTime = function(s){
	var second = parseInt(s);// 秒 
	var minute = 0;// 分 
	var hour = 0;// 小时 
	if(second > 60) {
		minute = parseInt(second/60);
		second = parseInt(second%60); 
	}
	if(minute > 60) { 
		hour = parseInt(minute/60);
		minute = parseInt(minute%60);
	}
	return hour + ":" + minute + ":" + second;
}